package com.hbusa.cotroller.read;

import com.hbusa.dto.TbChapter;
import com.hbusa.service.impl.*;
import com.hbusa.service.service.impl.ChapterServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/doNext")
public class nextServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ChapterServiceImpl chapterService = new ChapterServiceImpl();
        HttpSession session = req.getSession();
        int novelId = Integer.parseInt(req.getParameter("novelId"));
        ArrayList<TbChapter> tbChapters = chapterService.queryChapterByNovelId((long) novelId);
        int chapterId = Integer.parseInt(req.getParameter("chapterId"));
        System.out.println("传过来的章节id"+chapterId);
        TbChapter chapterById = null;
        for (TbChapter tbChapter : tbChapters) {
            long chapterId1 = tbChapter.getChapterId();
            int i = (int) chapterId1;
            if(i > chapterId)
            {
                System.out.println(i);
                chapterById = chapterService.findChapterById(i);
                System.out.println(chapterById.getChapterNum());
                break;
            }


        }
        session.setAttribute("chapter",chapterById);

        resp.sendRedirect("/read.jsp");
    }
}
